import time
from django.shortcuts import render, redirect, reverse
from django.http import JsonResponse, HttpResponse

from web import models
from utils.baidu.bos import create_bucket
from web.forms.project import ProjectModelForm


def project_list(request):
    """ 项目列表 """
    if request.method == 'POST':
        form = ProjectModelForm(request, data=request.POST)
        if form.is_valid():
            # 1.为项目创建一个桶(bucket注意字段要求) & 跨域规则
            bucket = '{}-{}-02110501'.format(request.tracer.user.mobile_phone, str(int(time.time())))
            region = 'fsh.bcebos.com'
            create_bucket(request, bucket, region)

            # 2.创建项目
            # 验证通过：项目名、颜色、描述 + creator谁创建的项目？

            # 没有request.tracer.user是进入不了后台管理页面的，
            # 所以不需要判断request.tracer.user是否有值
            form.instance.bucket = bucket
            form.instance.region = region
            form.instance.creator = request.tracer.user
            # 创建项目
            form.save()
            return JsonResponse({'status': True})
        return JsonResponse({'status': False, 'error': form.errors})

    """
    1. 从数据库中获取两部分数据 
        我创建的所有项目：已星标、未星标
        我参与的所有项目：已星标、未星标
    2. 提取已星标
        列表 = 循环 [我创建的所有项目] + [我参与的所有项目] 把已星标的数据提取

    得到三个列表：星标、创建、参与
    """
    project_dict = {'star': [], 'my': [], 'join': []}
    create_project_list = models.Project.objects.filter(creator=request.tracer.user)
    for row in create_project_list:
        if row.star:
            project_dict['star'].append({'value': row, 'type': 'my'})
        else:
            project_dict['my'].append(row)
    join_project_list = models.Project.objects.filter(projectuser__user=request.tracer.user)
    for item in join_project_list:
        if item.star:
            project_dict['star'].append({'value': item, 'type': 'join'})
        else:
            project_dict['join'].append(item)
    form = ProjectModelForm(request)
    return render(request, 'project_list.html', {'form': form, 'project_dict': project_dict})


def project_star(request, project_type, project_id):
    """星标项目"""
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=True)
        return redirect(reverse('web:project_list'))

    if project_type == 'join':
        models.Project.objects.filter(id=project_id, projectuser__user=request.tracer.user).update(star=True)
        return redirect(reverse('web:project_list'))

    return HttpResponse('请求错误')


def project_unstar(request, project_type, project_id):
    """取消星标"""
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        return redirect(reverse('web:project_list'))

    if project_type == 'join':
        models.Project.objects.filter(id=project_id, projectuser__user=request.tracer.user).update(star=False)
        return redirect(reverse('web:project_list'))

    return HttpResponse('请求错误')